package com.lyf.scm.admin.api.controller;


import com.alibaba.fastjson.JSONObject;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.service.DemandPlanService;
import com.lyf.scm.admin.domain.service.PermissionService;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import com.rome.user.common.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description:
 *           需求计划
 * @author: 李兵兵
 * @data: 2020/8/7 16:27
 */

@Slf4j
@Api(tags = "销售计划")
@RequestMapping("/api/v1/demandPlan")
@RomeController
public class DemandPlanController {

    @Resource
    private DemandPlanService demandPlanService;
    @Resource
    private PermissionService permissionService;

    @PostMapping("/listByWeek")
    @ApiOperation(value = "按条件查询需求计划")
    public Response<PlanResponseDTO<SalesPlanInfoDTO>> listWeekBySelective(@Valid @RequestBody SalesPlanDTO salesPlanDTO) {
        return Response.builderSuccess(demandPlanService.listWeekBySelective(salesPlanDTO));
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "查询需求范围指标(范围)")
    public Response<EChartsDTO> listAllByCycles(@RequestBody SalesPlanDTO salesPlanDTO){
        return Response.builderSuccess(demandPlanService.listAllByCycles(salesPlanDTO));
    }

    @PostMapping("/exportExcel")
    @ApiOperation("导出Excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "导出类型-1导出当前页-2导出选中数据-3导出所有数据", required = true, paramType = "query")
    })
    public Response<String> exportExcel(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type){
        log.info("销售计划导出接口，excelExportDTO={}，type={}", JSONObject.toJSONString(excelExportDTO), type);
        OperationLogRecordDTO operationLogRecordDTO = demandPlanService.saveOperatorLog(type);
        List<String> channelPermissionList = permissionService.getPermissionChannelList();
//        List<String> channelPermissionList = Arrays.asList("C00001");
//        List<SalesPlanSelectDTO> areaPermissionList = new ArrayList<>();
//        SalesPlanSelectDTO area1 = new SalesPlanSelectDTO();
//        area1.setCode("310000");
//        area1.setLevel(1);
//        area1.setName("上海");
//        area1.setParentCode("100000");
//        SalesPlanSelectDTO area2 = new SalesPlanSelectDTO();
//        area2.setCode("320000");
//        area2.setLevel(1);
//        area2.setName("江苏省");
//        area2.setParentCode("100000");
//        SalesPlanSelectDTO area3 = new SalesPlanSelectDTO();
//        area3.setCode("330000");
//        area3.setLevel(1);
//        area3.setName("浙江省");
//        area3.setParentCode("100000");
//        areaPermissionList.add(area1);
//        areaPermissionList.add(area2);
//        areaPermissionList.add(area3);
        List<SalesPlanSelectDTO> areaPermissionList = permissionService.getPermissionAreaList();
        demandPlanService.createExportFile(operationLogRecordDTO, excelExportDTO, type, channelPermissionList, areaPermissionList, UserContext.getUserId());
        return Response.builderSuccess("导出任务处理中，请至任务列表查看！");
    }
}
